/*
This software is contributed by Hong Jun from the Open Source Software
of Chinese Academy of Sciences. 

This software is distributed under the terms of the GNU General Public License.

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
*/
package ac.cas.oss;

import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;
import org.json.JSONObject;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;


/**
 *<b>Description:<b> 
 *Test program.
 *@author <a href="mailto:hongjun.bj@gmail.com">Hong Jun</a>
 *@since Nov 29, 2007 1:56:18 PM
 *@version 0.1
 */
public class Test {
	public static void main(String[] args) throws Exception {
		try {
			String urlStr = "http://A5338764333.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=A5338764333&citytype=1&unittype=1&lat=39.89945&long=116.40969";
			URLConnection feedUrl = new URL(urlStr).openConnection();
			feedUrl.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			SyndFeedInput input = new SyndFeedInput();
			SyndFeed feed = input.build(new XmlReader(feedUrl));
			String feedType = feed.getFeedType();// 获取原feed属于哪种格式
			System.out.println(feed.getTitle());
			System.out.println("----foreign:"
					+ feed.getForeignMarkup().toString());
			List list = (List) feed.getForeignMarkup();
			Iterator itr = (Iterator) list.iterator();
			Element ele = (Element) itr.next();
			Namespace ns = ele.getNamespace();
			Element ob = ele.getChild("ob", ns);
			JSONObject root = new JSONObject();
			if (ob != null) {
				root.put("temp", Float.parseFloat(ob.getChild("temp", ns)
						.getValue()));
				String str = ob.getChild("temp-high", ns).getValue();
				System.out.println("---temp high:" + str);
				root.put("temp_high", Float.parseFloat(str));
				ob.getChild("temp-low", ns).getValue();
			} else {
				System.out.println("aws:ob not exist!");
			}
			System.out.println(root.toString());
		} catch (Exception e) {
			System.out.println("-----Error:");
			e.printStackTrace();
		}
	}

}
